package com.amex.uam.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.amex.uam.service.NotifyService;
import com.amex.uam.template.TemplateManager;
import com.amex.uam.util.MailHelper;
import com.amex.uam.util.MailHelper.MailConf;
import com.amex.uam.util.Utils;

@Service("notifyService")
public class NotifyServiceImpl implements NotifyService {
	
	public static String GCS_EMAIL = "";
	public static String SMC_EMAIL = "";
	
	@Override
	public void asyncNotify(Action action, List<Product> prod, Map<String, String> params) {
		//if need email functionality, open this
		//new T(action, prod, params).start();
	}
	
	class T extends Thread {
		Action action;
		List<Product> prod;
		Map<String, String> params;
		
		public T(Action action, List<Product> prod, Map<String, String> params) {
			this.action = action;
			this.prod = prod;
			this.params = params;
		}
		@Override
		public void run() {
			new NotifyServiceImpl().notify(action, prod, params);
		}
	}
	
	@Override
	public void notify(Action action, List<Product> prod, Map<String, String> params) {
		//if need email functionality, open this
		/*if (action == null) {
			return;
		}
		if (prod == null || prod.isEmpty()) {
			System.out.println("no product selected");
			return;
		}
		List<MailConf> confList = null;
		switch(action) {
		case Registration:
			confList = handleRegistration(prod, params);
			break;
		case Approve:
			confList = handleApprove(prod, params);
			break;
		case Deny:
			confList = handleDeny(prod, params);
			break;
		case OptBlue:
			confList = handleOptBlue(prod, params);
			break;
		case GMAPS:
			confList = handleGmaps(prod, params);
			break;
		case Reminder:
			confList = handleReminder(prod, params);
			break; 
		}
		
		if (confList != null) {
			for (Iterator<MailConf> it = confList.iterator(); it.hasNext();) {
				MailConf conf = it.next();
				MailHelper.dispatchEmail(conf);
			}
		}*/
	}
	
	private List<MailConf> handleRegistration(List<Product> prod, Map<String, String> params) {
		
		// get parameters 
		Calendar laterDate = Calendar.getInstance();
		laterDate.add(Calendar.DAY_OF_MONTH,45);
		DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");     
		String strLaterDate = sdf.format(laterDate);
		
		String userId =  Utils.trim(params.get("userId"), "");
		String accessType = Utils.trim(params.get("accessType"), "");
		String costCenter = Utils.trim(params.get("costCenter"), "");
		String reason = Utils.trim(params.get("reason"), "");
		String middleInitial = Utils.trim(params.get("middleInitial"), "");
		String firstName = Utils.trim(params.get("firstName"), "");
		String lastName = Utils.trim(params.get("lastName"), "");
		String fullName = (middleInitial.length() > 0)	//#FULL_NAME# 
				?  firstName + " " + lastName
				: firstName + " " + middleInitial + " " + lastName;
		String mFirstName = Utils.trim(params.get("mFirstName"), ""); // #MANAGER_FIRST_NAME#
		
		String userEmail = Utils.trim(params.get("userEmail"), "");
		String managerEmail = Utils.trim(params.get("userEmail"), "");
		
		String product = ""; // #PRODUCT#
		for (int i = 0; i < prod.size(); i++) {
			if (i > 0) {
				product += " and ";
			} 
			product += prod.toString();
		}

		// build the mail subject
		String subject = "URGENT ACTION - Request for Approval - " + product + "  Access Request for " + fullName;
		// build the mail body
		String body = TemplateManager.getTemplate(TemplateManager.TEMPLATE_Registration);
		body = body.replace("#SUBJECT#", subject)
				.replace("#MANAGER_FIRST_NAME#", mFirstName)
				.replace("#FULL_NAME#", fullName)
				.replace("#PRODUCT#", product)
				.replace("#USER_ID#", userId)
				.replace("#ACCESS_TYPE#", accessType)
				.replace("#COST_CENTER#", costCenter)
				.replace("#REASON#", reason)
				.replace("#LATER_DATE#", strLaterDate);
		
		MailConf conf = new MailConf();
		conf.setFrom(userEmail);
		conf.setTo(managerEmail);
		conf.setCc(userEmail + "," + GCS_EMAIL );
		conf.setSubject(subject);
		conf.setContent(body);
		
		List<MailConf> list = new ArrayList<MailConf>();
		list.add(conf);
		return list;
	}
	
	private List<MailConf> handleGmaps(List<Product> prod, Map<String, String> params) {
		String userEmail = Utils.trim(params.get("userEmail"), "");
		String managerEmail = Utils.trim(params.get("userEmail"), "");
		
		MailConf conf = new MailConf();
		conf.setFrom(managerEmail);
		conf.setTo(userEmail);
		conf.setCc(userEmail + "," + GCS_EMAIL );
		
		//XXX how to build the subject and the body
		conf.setSubject("GMAPS Subject");
		conf.setContent("GMAPS Body");
		
		List<MailConf> list = new ArrayList<MailConf>();
		list.add(conf);
		return list;
	}

	private List<MailConf> handleOptBlue(List<Product> prod, Map<String, String> params) {
		String userEmail = Utils.trim(params.get("userEmail"), "");
		String managerEmail = Utils.trim(params.get("userEmail"), "");
		
		MailConf conf = new MailConf();
		conf.setFrom(managerEmail);
		conf.setTo(userEmail);
		conf.setCc(userEmail + "," + SMC_EMAIL );
		
		//XXX how to build the subject and the body
		conf.setSubject("OptBlue Subject");
		conf.setContent("OptBlue Body");
		
		List<MailConf> list = new ArrayList<MailConf>();
		list.add(conf);
		return list;
	}

	private List<MailConf> handleDeny(List<Product> prod, Map<String, String> params) {
		String userEmail = Utils.trim(params.get("userEmail"), "");
		String managerEmail = Utils.trim(params.get("userEmail"), "");
		
		MailConf conf = new MailConf();
		conf.setFrom(managerEmail);
		conf.setTo(userEmail);
		conf.setCc(userEmail + "," + SMC_EMAIL );
		
		//XXX how to build the subject and the body
		conf.setSubject("Deny Subject");
		conf.setContent("Deny Body");
		
		List<MailConf> list = new ArrayList<MailConf>();
		list.add(conf);
		return list;
	}

	private List<MailConf> handleApprove(List<Product> prod, Map<String, String> params) {
		
		String userEmail = Utils.trim(params.get("userEmail"), "");
		String managerEmail = Utils.trim(params.get("userEmail"), "");
		
		MailConf conf = new MailConf();
		conf.setFrom(managerEmail);
		conf.setTo(userEmail);
		conf.setCc(userEmail + "," + SMC_EMAIL );
		
		//XXX how to build the subject and the body
		conf.setSubject("Approve Subject");
		conf.setContent("Approve Body");
		
		List<MailConf> list = new ArrayList<MailConf>();
		list.add(conf);
		return list;
	}
	private List<MailConf> handleReminder(List<Product> prod, Map<String, String> params) {
		// TODO Auto-generated method stub
		return null;
	}
}
